//
//  QualityReportCell.swift
//  QualityControlSystem
//
//  Created by dundun on 17/1/11.
//  Copyright © 2017年 com.mask. All rights reserved.
//

import UIKit
import SnapKit

class QualityReportCell: UITableViewCell {

    var checkTitle: UILabel = UILabel()
    var checkContent: UILabel = UILabel()
    var checkState: UIImageView = UIImageView()
    var failureIcon = UIImageView()
    var checkReason: UILabel = UILabel()
    
    // MARK: - set
    
    var model: QualityReport? {
        didSet {
            checkTitle.text = model?.checkTitle
            checkContent.text = model?.checkContent
            checkState.backgroundColor = UIColor.red
            failureIcon.backgroundColor = UIColor.blue
            checkReason.text = model?.checkReason
            layoutSelfSubviews()
            
            checkTitle.backgroundColor = UIColor.red
            checkContent.backgroundColor = UIColor.green
            checkReason.backgroundColor = UIColor.blue
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        setupSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupSubviews() {
        setupLabel(label: checkTitle, fontSize: 15, color: UIColor.black)
        setupLabel(label: checkContent, fontSize: 13, color: UIColor.gray)
        setupLabel(label: checkReason, fontSize: 13, color: UIColor.black)
        contentView.addSubview(checkState)
        contentView.addSubview(failureIcon)
    }
    
    func setupLabel(label: UILabel, fontSize: CGFloat, color: UIColor) {
        label.font = UIFont.systemFont(ofSize: fontSize)
        label.textColor = color
        contentView.addSubview(label)
    }
    
    func layoutSelfSubviews() {
        if model?.isQualified == true {
            qualifiedLayout()
        } else if model?.isQualified == false {
            unqualifiedLayout()
        }
    }
    
    ///不合格的布局
    func unqualifiedLayout() {
        checkTitle.frame = CGRect(x: 15.0, y: contentView.frame.minY + 20.0, width: 100.0, height: 20.0)
        checkContent.frame = CGRect(x: 15.0, y: checkTitle.frame.maxY + 20.0, width: 100.0, height: 20.0)
        failureIcon.frame = CGRect(x: 15.0, y: checkContent.frame.maxY + 15.0, width: 30.0, height: 30.0)
        checkReason.frame = CGRect(x: failureIcon.frame.maxX, y: checkContent.frame.maxY + 20.0, width: 100.0, height: 20.0)
        
        checkState.snp.makeConstraints { (make) in
            make.right.equalTo(contentView.snp.right).offset(-15.0)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: 30.0, height: 30.0))
        }
    }
    
    //合格的布局
    func qualifiedLayout() {
        
        checkTitle.frame = CGRect(x: 15.0, y: contentView.frame.minY + 20.0, width: 100.0, height: 20.0)
        checkContent.frame = CGRect(x: 15.0, y: checkTitle.frame.maxY + 20.0, width: 100.0, height: 20.0)
        failureIcon.frame = CGRect(x: 15.0, y: checkContent.frame.maxY + 15.0, width: 0.0, height: 0.0)
        checkReason.frame = CGRect(x: failureIcon.frame.maxX, y: checkContent.frame.maxY + 0.0, width: 0.0, height: 0.0)
            
        checkState.snp.makeConstraints { (make) in
            make.right.equalTo(contentView.snp.right).offset(-15.0)
            make.centerY.equalTo(contentView.snp.centerY)
            make.size.equalTo(CGSize(width: 30.0, height: 30.0))
        }
    }

}
